<?php
require_once "BaseSQLDAO.php";
require_once dirname(__FILE__)."/../UserDAO.php";
require_once "SQLStatements.php";
require_once "ErrorMessages.php";
require_once dirname(__FILE__)."/../DataAccessException.php";
require_once dirname(__FILE__)."/../../model/User.php";

class UserSQLDAOImpl extends BaseSQLDAO implements UserDAO {

    public function create($object) {
        $stmt = $this->getConnection()->prepare(SQLStatements::USER_INSERT);
        $stmt->bind_param("isss", $object->getId(), $object->getUid(), $object->getUserName(), $object->getMobileNumber());
        $stmt->execute();
        if ($stmt->affected_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_CREATE_ERROR);
        }
    }

    public function read($id) {
        $user = new User();
        $stmt = $this->getConnection()->prepare(SQLStatements::USER_SELECT_BY_ID);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->bind_result($userId, $userUid, $userName, $mobileNumber);
        $stmt->store_result();
        $stmt->fetch();
        if ($stmt->num_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_READ_ERROR);
        }
        $user->setId($userId);
        $user->setUid($userUid);
        $user->setUserName($userName);
        $user->setMobileNumber($mobileNumber);
        return $user;
    }

    public function getByUid($uid) {
        $user = new User();
        $stmt = $this->getConnection()->prepare(SQLStatements::USER_SELECT_BY_UID);
        $stmt->bind_param("s", $uid);
        $stmt->execute();
        $stmt->bind_result($userId, $userUid, $userName, $mobileNumber);
        $stmt->store_result();
        $stmt->fetch();
        if ($stmt->num_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_READ_ERROR);
        }
        $user->setId($userId);
        $user->setUid($userUid);
        $user->setUserName($userName);
        $user->setMobileNumber($mobileNumber);
        return $user;
    }

    public function update($object) {
        $stmt = $this->getConnection()->prepare(SQLStatements::USER_UPDATE);
        $stmt->bind_param("sssi", $object->getUid(), $object->getUserName(), $object->getMobileNumber(), $object->getId());
        $stmt->execute();
        if ($stmt->affected_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_UPDATE_ERROR);
        }
    }

    public function delete($id) {
        $stmt = $this->getConnection()->prepare(SQLStatements::USER_DELETE_BY_ID);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        if ($stmt->affected_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_DELETE_ERROR);
        }
    }

    public function deleteAll() {
        $stmt = $this->getConnection()->prepare("DELETE FROM user");
        $stmt->execute();
    }

    public function count() {
        $stmt = $this->getConnection()->prepare("SELECT COUNT(user_id) FROM user");
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();
        return $count;
    }

    public function countUid($uid) {
        $stmt = $this->getConnection()->prepare(SQLStatements::USER_COUNT_UID);
        $stmt->bind_param("s", $uid);
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();
        return $count;
    }

    public function countUserName($userName) {
        $stmt = $this->getConnection()->prepare(SQLStatements::USER_COUNT_USER_NAME);
        $stmt->bind_param("s", $userName);
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();
        return $count;
    }

}
?>
